﻿#region using
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using AwesomeTanks.Sources.Tools;
using AwesomeTanks.Sources.Shape;
using System.Collections.Generic;
using AwesomeTanks.Sources.Screens;
using AwesomeTanks.Sources.Particles;
using ShapeBlaster;
using AwesomeTanks.Sources.Media;
using System;
#endregion

namespace AwesomeTanks.Sources.GameElements.ShootElements
{
    /// <summary>
    /// Model of a Shoot in AwesomeTanks. A shoot derives from MovableSprite
    /// </summary>
    public class Shoot : MovableSprite
    {
        #region Fields

        public const float SHOOT_LAYER_DEPTH = 1f;

        public int Damages { get; set; }
        public Player Shooter { get; set; }
        public bool ToRemove { get; set; }
        public List<Circle> HitboxList { get; set; }
        public ShootType ShootType { get; set; }
        public bool AlreadyHit { get; set; }
        public static Random rand = new Random();

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Shoot"/> class.
        /// </summary>
        /// <param name="position">The initial position of the shoot.</param>
        /// <param name="speed">The speed of the shoot.</param>
        /// <param name="texture">The texture of the shoot.</param>
        /// <param name="sourceRectangle">The source rectangle to display in the texture.</param>
        /// <param name="damages">The base damages of the shoot.</param>
        /// <param name="shooter">The shooter.</param>
        public Shoot(Vector2 position, Vector2 speed, Texture2D texture, Rectangle sourceRectangle, int damages, Player shooter, ShootType shootType)
            : base(texture, sourceRectangle, position, speed)
        {
            this.LayerDepth = SHOOT_LAYER_DEPTH;
            this.Origin = new Vector2(sourceRectangle.Width / 2, sourceRectangle.Height / 2);
            this.Damages = damages;
            this.Shooter = shooter;
            this.Rotation = speed.ToAngle();
            this.ToRemove = false;
            this.ShootType = shootType;
            this.AlreadyHit = false;

            // Initialize hitboxes
            this.HitboxList = new List<Circle>();
            if (shootType == ShootType.GATLING)
            {
                this.HitboxList.Add(new Circle(position, this.SourceRectangle.Value.Width));
            }
            else {
                this.HitboxList.Add(new Circle(position, this.SourceRectangle.Value.Width / 2));
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Add particles when the shoot hit a target.
        /// </summary>
        public void Hit()
        {
            float hue1 = rand.NextFloat(0, 6);
            float hue2 = (hue1 + rand.NextFloat(0, 2)) % 6f;
            Color color1 = ColorUtil.HSVToColor(hue1, 0.5f, 1);
            Color color2 = ColorUtil.HSVToColor(hue2, 0.5f, 1);
            /*
            float hue1 = ColorUtil.ColorToHSV(Color.Yellow).X;
            float hue2 = (hue1 + rand.NextFloat(0, 2)) % 6f;
            Color color1 = ColorUtil.HSVToColor(hue1, 0.5f, 1);
            Color color2 = ColorUtil.HSVToColor(hue2, 0.5f, 1);
            */
            for (int i = 0; i < 50; i++)
            {
                float speed = 18f * (1f - 1 / rand.NextFloat(1, 10));
                var state = new ParticleState()
                {
                    Velocity = rand.NextVector2(speed, speed),
                    Type = ParticleType.Enemy,
                    LengthMultiplier = 1
                };

                Color color = Color.Lerp(color1, color2, rand.NextFloat(0, 1));
                GameplayScreen.ParticleManager.CreateParticle(Art.SpriteSheet, Position, new Rectangle(Art.ParticlePosition.X, Art.ParticlePosition.Y, Art.ParticleDimension.X, Art.ParticleDimension.Y), color, 10, 1.5f, state);
            }
        }

        public void RocketHit()
        {
            float hue1 = rand.NextFloat(0, 6);
            float hue2 = (hue1 + rand.NextFloat(0, 2)) % 6f;
            Color color1 = ColorUtil.HSVToColor(hue1, 0.5f, 1);
            Color color2 = ColorUtil.HSVToColor(hue2, 0.5f, 1);

            for (int i = 0; i < 200; i++)
            {
                float speed = 18f * (1f - 1 / rand.NextFloat(1, 10));
                var state = new ParticleState()
                {
                    Velocity = rand.NextVector2(speed, speed),
                    Type = ParticleType.Enemy,
                    LengthMultiplier = 3
                };

                Color color = Color.Lerp(color1, color2, rand.NextFloat(0, 1));
                GameplayScreen.ParticleManager.CreateParticle(Art.SpriteSheet, Position, new Rectangle(Art.ParticlePosition.X, Art.ParticlePosition.Y, Art.ParticleDimension.X, Art.ParticleDimension.Y), color, 300, 1.5f, state);
            }
        }


        #endregion

        #region Update and Draw
        /// <summary>
        /// Updates the Shoot in function of the specified game time.
        /// </summary>
        /// <param name="gameTime">A snapshot of the current time.</param>
        public override void Update(GameTime gameTime)
        {
            //Call to the MovableSprite's Update function
            base.Update(gameTime);

            // Update hitbox
            this.HitboxList[0].Center = this.Position;
        }

        /// <summary>
        /// Draws this Shoot in the specified sprite batch.
        /// </summary>
        /// <param name="spriteBatch">The sprite batch.</param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            //Call to the MovableSprite's Draw function
            base.Draw(spriteBatch);
        }
        #endregion
    }
}
